package cn.doitedu.javase.socket.demo4;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class _02_聊天客户端 {

    public static void main(String[] args) throws IOException {

        Socket socket = new Socket("localhost", 8888);

        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));

        Scanner scanner = new Scanner(System.in);
        // 从控制台获取一条消息
        String msg = scanner.nextLine();

        // 发送这条消息给服务端
        bw.write(msg);
        bw.newLine();
        bw.flush();

        String line;
        while( (line = br.readLine())!= null){
            System.out.println("服务端： " + line);

            // 给服务端再发消息
            msg = scanner.nextLine();
            bw.write(msg);
            bw.newLine();
            bw.flush();
        }

    }

}
